<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // function Teacher() {
    //   this.profession = { a: 1 }
    // }
    // var teacher = new Teacher()
    // Student.prototype = teacher
    // // Studend改变会影响到teacher
    // // 因为Student.prototype来自Teacher.prototype
    // Student.prototype.age = 11
    // function Student() {
    // }
    // var student = new Student()
    // console.log(student)
    // console.log(teacher)




    // function Teacher() {
    //   this.profession = { a: 1 }
    // }
    // var teacher = new Teacher()

    // // 中间加一层
    // Buffer.prototype = teacher
    // function Buffer() { }
    // var buffer = new Buffer()


    // Student.prototype = buffer
    // // age是加在了buffer上
    // Student.prototype.age = 11
    // function Student() {
    // }
    // var student = new Student()
    // console.log(student)
    // console.log(buffer)
    // console.log(teacher)



    //  解决继承中子影响父的问题
    // 封装一个中间过渡
    // var inherit = (function () {
    //   var Buffer = function () { }
    //   return function (Target, Origin) {
    //     Buffer.prototype = Origin.prototype;
    //     Target.prototype = new Buffer();
    //     Target.prototype.constructor = Target;
    //     Target.prototype.super_calss = Origin;
    //   }
    // })()
    // Teacher.prototype.name = "Mr. zhang"
    // function Teacher() { }
    // function Student() { }
    // inherit(Student, Teacher)

    // // 因为在inherit中重写了Student的prototype,所以要在之后添加的属性才能生效
    // Student.prototype.aaa = "ST. zhang"

    // var s = new Student()
    // var t = new Teacher()
    // console.log(s)
    // console.log(t)



    // 实战 1
    // var inherit = (function () {
    //   var Buffer = function () { }
    //   return function (Target, Origin) {
    //     Buffer.prototype = Origin.prototype;
    //     Target.prototype = new Buffer();
    //     Target.prototype.constructor = Target;
    //     Target.prototype.super_calss = Origin;
    //   }
    // })()
    // var initProgrammer = (function () {
    //   var Programmer = function () { }
    //   Programmer.prototype = {
    //     name: '程序员',
    //     say: function () {
    //       console.log(`${this.direction}${this.name}需要会${this.skill.join('、')}`)
    //     }
    //   }
    //   function FrontEnd() { }
    //   function BackEnd() { }
    //   inherit(FrontEnd, Programmer)
    //   inherit(BackEnd, Programmer)
    //   FrontEnd.prototype.direction = '前端'
    //   BackEnd.prototype.direction = '后端'
    //   FrontEnd.prototype.skill = ['html', 'css', 'js']
    //   BackEnd.prototype.skill = ['node', 'java', 'php']
    //   return {
    //     FrontEnd,
    //     BackEnd
    //   }
    // })()
    // var frontEnd = new initProgrammer.FrontEnd()
    // var backEnd = new initProgrammer.BackEnd()
    // frontEnd.say()
    // backEnd.say()


    // 与立即执行函数相比
    // 这种方式是把写好的构造函数存放在一个变量中，用的时候按需取
    // 立即执行函数。挂载在window全局上，




    // 加塞实验
    // var a = [1, 2, 3]
    // var b = a;
    // var c = b;
    // c.push(4)
    // console.log(a, b, c)// a,b都被改掉


    var inherit = (function () {
      var Buffer = function () { }
      return function (Target, Origin) {
        Buffer.prototype = Origin.prototype;
        Target.prototype = new Buffer();
        Target.prototype.constructor = Target;
        Target.prototype.super_calss = Origin;
      }
    })()

    var initFuncs = (function () {
      function Funcs() {

      }
      Funcs.prototype.oneF = function () {
        for (var i = this.num; i > 0; i--) {
          if (i % 3 == 0 && i % 5 == 0 && i % 7 == 0) {
            console.log(i)
          }
        }
      }
      Funcs.prototype.twoF = function () {
        var a = 0, b = 1, c = 0;
        for (var i = 0; i < this.num - 2; i++) {
          c = a + b;
          a = b;
          b = c
        }
        return c
      }
      Funcs.prototype.threeF = function () {
        var num = 0
        for (var i = 0; i < this.num + 1; i++) {
          num += i
        }
        return num
      }

      function OneF(num) {
        this.num = num
      }
      function TwoF(num) {
        this.num = num
      }
      function ThreeF(num) {
        this.num = num
      }
      inherit(OneF, Funcs)
      inherit(TwoF, Funcs)
      inherit(ThreeF, Funcs)

      return {
        OneF, TwoF, ThreeF
      }
    })()

    var onef = new initFuncs.OneF(200)
    onef.oneF()
    var twof = new initFuncs.TwoF(5)
    console.log(twof.twoF())
    var threeF = new initFuncs.ThreeF(10)
    console.log(threeF.threeF())
  </script>
</body>

</html>